package com.dao.impl;
// default package

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.dao.ExamDao;
import com.model.ClazzProanswer;
import com.model.Proanswer;
import com.model.Programming;

/**
 	* A data access object (DAO) providing persistence and search support for PaperProgram entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .PaperProgram
  * @author MyEclipse Persistence Tools 
 */
@Repository("examDao")
public class ExamDaoImpl implements ExamDao {
	@Resource
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<Programming> getProgramList(int paperId,Integer uid,String flag) {
		
		String hql="select  pro from Programming pro, PaperProgram pp where :pid = pp.paper.id and pp.programming.id=pro.id";
		List<Programming> list= new ArrayList<Programming>();
		Session session = sessionFactory.getCurrentSession();
		list=session.createQuery(hql).setInteger("pid",paperId).list();
		
		List answerList = new ArrayList();
		List<Integer> lst = new ArrayList();
		List<Programming> ret= new ArrayList<Programming>();
		if(flag.equals("user")){
			answerList = getAnswerByid(paperId,uid);
			for(int i=0;i<answerList.size();i++){
				for(int j=0;j<list.size();++j){
					Proanswer pro =(Proanswer) answerList.get(i);
					Programming pm = list.get(j);
					if(pro.getProgramming().getId() == pm.getId()){
						//pm.setAnswer(pro.getAnswer());
						if(pro.getScore()>0){
							lst.add(j);
						}
					}	
					
				}
			}
		}else{
			answerList = getClazzAnswerByid(paperId,uid);
			
			for(int i=0;i<answerList.size();i++){
				for(int j=0;j<list.size();j++){
					ClazzProanswer pro =(ClazzProanswer) answerList.get(i);
					Programming pm = list.get(j);
					if(pro.getProgramming().getId() == pm.getId()){
						//pm.setAnswer(pro.getAnswer());
						if(pro.getScore()>0){
							lst.add(j);
						}
					}	
					
				}
			}
		}
		
		HashSet<Integer> hs = new HashSet();
		for(int i = 0 ; i < lst.size() ;++i){
			hs.add(lst.get(i));
		}
		for(int i = 0 ; i < list.size() ;++i){
			if(!hs.contains(i))
				ret.add(list.get(i));
		}
		
		return ret;
	}
	
	public List<Proanswer> getAnswerByid(int pid, int uid){
		String hql = "from Proanswer pro where pro.paper.id = "+pid+" and pro.user.id = "+uid;
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery(hql);
		return q.list();
	}
	
	public List<ClazzProanswer> getClazzAnswerByid(int pid, int uid){
		String hql = "from ClazzProanswer pro where pro.paper.id = "+pid+" and pro.student.id = "+uid;
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery(hql);
		return q.list();
	}

	@Override
	public void saveAnswer(Proanswer proanswer) {
		Session session = sessionFactory.getCurrentSession();
		
		int proId = proanswer.getProgramming().getId();
		int pid = proanswer.getPaper().getId();
		int uid = proanswer.getUser().getId();
		String hql = "from Proanswer pro where pro.programming.id ="+proId+" and pro.user.id="+uid+" and pro.paper.id="+pid;
		Query query = session.createQuery(hql);
		
		if(!query.list().isEmpty()){
			Proanswer pro = (Proanswer) query.list().get(0);
			pro.setAnswer(proanswer.getAnswer());
			pro.setScore(proanswer.getScore());
			if(proanswer.getScore()>0){
				pro.setSubmittime(new Timestamp(System.currentTimeMillis()));
			}
			proanswer=pro;
		}		
		session.save(proanswer);		
	}

	@Override
	public void classSaveAnswer(ClazzProanswer clazzProanswer) {
		Session session = sessionFactory.getCurrentSession();
		
		int proId = clazzProanswer.getProgramming().getId();
		int pid = clazzProanswer.getPaper().getId();
		int sid = clazzProanswer.getStudent().getId();
		String hql = "from ClazzProanswer pro where pro.programming.id ="+proId+" and pro.student.id="+sid+" and pro.paper.id="+pid;
		Query query = session.createQuery(hql);
		
		if(!query.list().isEmpty()){
			ClazzProanswer pro = (ClazzProanswer) query.list().get(0);
			pro.setAnswer(clazzProanswer.getAnswer());
			pro.setScore(clazzProanswer.getScore());
			clazzProanswer=pro;
		}
		session.save(clazzProanswer);	
	}


	


}